package com.tarena.lbs.article.web.controller;

import com.tarena.lbs.article.web.service.ArticleService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.pojo.content.param.ArticleActionParam;
import com.tarena.lbs.pojo.content.param.ArticleContentParam;
import com.tarena.lbs.pojo.content.query.ArticleQuery;
import com.tarena.lbs.pojo.content.vo.ArticleActionPageResultVO;
import com.tarena.lbs.pojo.content.vo.ArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

/**
 * 和文章业务有关的所有接口功能
 */
@RestController
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    //前台小程序查询 上限的 在附近范围的 某个标签下的文章
    //后台管理查询 有分页业务条件的 当前后台登录用户的所属文章
    @GetMapping("/admin/content/article/list")
    public Result<PageResult<ArticleVO>> articleList(ArticleQuery query)
        throws BusinessException{
        return new Result<>(articleService.articleList(query));
    }
    @PostMapping("/admin/content/article/insert")
    public Result<Void> addArticle(ArticleContentParam param) throws BusinessException {
        articleService.addArticle(param);
        return Result.success();
    }
    //前台查询接口 查询某个中心点定位的范围内文章标签集合
    @GetMapping("/admin/content/article/getArticleLabel")
    public Result<Set<String>> getArticleLabels(ArticleQuery query)
        throws BusinessException{
        return new Result<>(articleService.getArticleLabels(query));
    }

    //根据文章的id 查询获取文章详情
    @GetMapping("/admin/content/article/get")
    public Result<ArticleVO> articleDetail(Integer id)
        throws BusinessException{
        return new Result<>(articleService.articleDetail(id));
    }
    //用户点赞收藏行为
    @PostMapping("/admin/content/article/behavior")
    public Result<Void> userBehave(ArticleActionParam param)
        throws BusinessException{
        articleService.userBehave(param);
        return Result.success();
    }
    //查询某个用户曾经点赞和收藏过的文章
    @GetMapping("/admin/content/article/getBehaviorList")
    public Result<ArticleActionPageResultVO> getBehaviorList(ArticleQuery query)
        throws BusinessException{
        long start=System.currentTimeMillis();
        ArticleActionPageResultVO behaviorList = articleService.getBehaviorList(query);
        long end=System.currentTimeMillis();
        log.info("查询用户行为列表耗时：{}MS",end-start);
        return new Result<>(behaviorList);
    }
}
